<? defined('SYSPATH') or die('No direct script access.');

class Controller_Ajax extends Controller {

    public function action_loadimg()
    {
        if($this->request->is_ajax()) {
            $items_per_page = 12;
            $items = ORM::factory('file');
            $total  = $items->reset(FALSE)->count_all();
            $page   = ($this->request->query('page')) ? $this->request->query('page') : 1;
            $offset = ($page - 1) * $items_per_page;
            $items = $items->order_by('uploaded', 'desc')
                ->limit($items_per_page)
                ->offset($offset)
                ->find_all();
            $prev = ($page == 1)?false:$page-1;
            $next = ($items_per_page*$page <= $total)?$page+1:false;
            echo View::factory('gallery')
                ->set('items', $items->as_array())
                ->set('prev', $prev)
                ->set('next', $next);
        }
        else
            throw new HTTP_Exception_404();
    }

}
